<?php 
	
namespace app\shop\controller;
use think\Db; // 引用DB类
use think\Request;
use think\Session;

class Shop
{

    // http://127.0.0.1:85/index.php?s=/tdled/test
    /*
    {
        "GDH": "AK1970254",
        "LSM": "002"
    }
	*/
    public function index(){
    	$shop_name		= request()->post("shop_name");
    	$shop_wangwang	= request()->post("shop_wangwang");
    	$shop_type		= request()->post("shop_type");
    	$shop_img		= request()->file('file');
    	
    	var_dump($shop_img);
    }
    
    /*
     * 2020年05月15日
     * 1、新增店铺
     * 2、判断店铺是否已经存在
     * */
    public function isShop($id){
    	$isShop	= Db::table("shop")
    		   -> where("shop_name",$id)
    		   -> select();
    	if($isShop){
    		return json(["msg"=>"店铺已存在，请重新添加","code"=>200]);
    	}
    }
    
    //先上传图片，随后将图片地址回传
    public function shopImg(){
    	$shop_img		= request()->file('file');
    	$info 			= $shop_img->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'shop_img');
    	
    	// 图片保存后会根据日期再生成一个以上传日期命名的文件夹，要将路径存入数据库
    	$date 			= '20'.date('ymd'); 
    	$shop_img_url 	=  "http://127.0.0.1:88/static/shop_img/".$date."/".$info->getFilename();
    	
    	//将图片地址存入数据库
    	$isSave			= Db::table("shop_img")
    				   -> insert([
    				   		"shop_img_url"		=> $shop_img_url,
    				   		"shop_img_addTime"	=> Db::raw("now()")
    				   ]);
    	if($isSave){
    		return json(["url"=>$shop_img_url,"code"=>200]);
    	}else{
    		return json(["url"=>$shop_img_url,"code"=>500]);
    	}
    	
    }
    //添加店铺
    public function addShop(){
    	$shop_name		= request()->post("shop_name");
    	$shop_wangwang	= request()->post("shop_wangwang");
    	$shop_type		= request()->post("shop_type");
    	$user_name		= request()->post("user_name");
    	$shop_img_url	= request()->post('shop_img_url');
    	$shop_info		= Db::table("shop")
    				   -> insert([
    				   		"shop_name"		=> $shop_name,
    				   		"shop_wangwang"	=> $shop_wangwang,
    				   		"shop_type"		=> $shop_type,
    				   		"shop_img_url"	=> $shop_img_url,
    				   		"shop_addTime"	=> Db::raw("now()"),
    				   		"user_name"		=> $user_name
    				   ]);
    	if($shop_info){
    		return json(["code"=>200,"msg"=>"添加成功"]);
    	}else{
    		return json(["code"=>500,"msg"=>"添加失败，请重试"]);
    	}
    }
    
    //单个删除店铺
    public function delShop($id){
    	
    	$isDel	= Db::table("shop")
    		   -> where("shop_name",$id)
    		   -> delete();
    	/*$isDel	= Db::table("shop")
    		   -> alias("s")
    		   -> join("shop_img sm","s.shop_img_url=sm.shop_img_url")
    		   -> where("s.shop_name",$id)
    		   -> delete();*/
    	//$isDel	= Db::query("delete s,si from shop s left join shop_img si on s.shop_img_url=si.shop_img_url where s.shop_name='".$id."'");
    	
    	if($isDel){
    		return json(["msg"=>"删除成功","code"=>200]);
    	}else{
    		return json(["msg"=>"请重试","code"=>500]);
    	}
    	//return json(["msg"=>$id]);
    }
    
    //批量删除店铺
	public function delMoreShop($shop_name){
		
		foreach($shop_name as $shopname){
			$del	= Db::table('shop')
			   	   -> where('shop_name', $shopname)
			   	   -> delete();
		}
	}
	
	//店铺搜索功能
	public function shopSearch(){
		$key		= request()->post("key");
		$user_name	= request()->post("user_name");
		$isShop		= Db::table("shop")
				   -> where("user_name",$user_name)
				   -> where("shop_name|shop_wangwang|shop_type","like","%".$key."%")
				   -> select();
		$count		= count($isShop);		   
		return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$isShop]);
	}
	
	//店铺详细信息
	public function detailShop($id){
		$detail		= Db::table('shop')
				   -> where("shop_name",$id)
				   -> select();
		if($detail){
			return json(["detail"=>$detail]);
		}
	}
    
    //店铺审核
    public function checkShop(){
    	$shop_name		= request()->post("shop_name");
    	$user_name		= request()->post("user_name");
    	$shop_check		= request()->post("shop_check");
    	$check_desc		= request()->post("check_desc");
    	
    	//var_dump($user_name);
    	
    	$check			= Db::table("shop")
    				   -> where("shop_name",$shop_name)
    				   -> update(
    				   		[
    				   			"shop_isCheck"	=> $shop_check,
    				   			"check_desc"	=> $check_desc
    				   		]
    				   );
    	var_dump($check);
    }
    
    //编辑店铺
    public function shopEdit(){
    	$shop_name		= request()->post("shop_name");
    	$shop_wangwang	= request()->post("shop_wangwang");
    	$shop_type		= request()->post("shop_type");
    	$user_name		= request()->post("user_name");
    	$shop_img_url	= request()->post('shop_img_url');
    	$shop_id		= request()->post('shop_id');
    	
    	$shop_info		= Db::table("shop")
    				   -> where("shop_id",$shop_id)
    				   -> where("user_name",$user_name)
    				   -> update([
    				   		"shop_name"		=> $shop_name,
    				   		"shop_wangwang"	=> $shop_wangwang,
    				   		"shop_type"		=> $shop_type,
    				   		"shop_img_url"	=> $shop_img_url,
    				   		"shop_addTime"	=> Db::raw("now()")
    				   ]);
    	if($shop_info){
    		return json(["code"=>200,"msg"=>"添加成功"]);
    	}else{
    		return json(["code"=>500,"msg"=>"添加失败，请重试"]);
    	}
    }
    //返回商铺给前端list
    public function shopList($id){
    	$role		= Db::table("login")
    			   -> where("login_name",$id)
    			   -> select();
    	//var_dump($role);
    	if($role[0]["register_role"]==2){
    		$shopList	= Db::table("shop")
	    			   -> where("user_name",$id)
	    			   -> select();
	    	$count		= count($shopList);
    		
    	}else{
    		$shopList	= Db::table("shop")
	    			   -> select();
	    	$count		= count($shopList);
    	}
    	
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$shopList]);
    }
}
